import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'node:path';
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  return {
    plugins: [
      react(
        // {
        //   babel: {
        //     // presets: ['@babel/preset-flow'],
        //     plugins: ['@babel/plugin-transform-flow-strip-types']
        //   }
        // }
      ),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src/')
      }
    },
    server: {
      port: 5177,
      proxy: {
        // 用于sse
        '/api': {
            target: 'http://localhost:3000/api',
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, '')
        },
    }
    },
    build: {
      outDir: `dist${mode ? '-' : ''}${mode}`,
      // https://cn.rollupjs.org/configuration-options/
      rollupOptions: {
        output: {
          manualChunks(id) {
            // https://cn.rollupjs.org/configuration-options/#output-manualchunks
            if (id.includes('node_modules/antd')) {
              // console.log('name:', id.split('node_modules/antd/es/')[1].split('/')[0]);
              const moduleName = id.split('node_modules/antd/es/')[1].split('/')[0];
              if (moduleName.charCodeAt(0) < 'm'.charCodeAt(0)) {
                return 'antd-chunk-1';
              } else {
                return 'antd-chunk-2';
              }
              // return 'antd'
            }

            //react相关的打进一个包
            if (id.includes('node_modules/react')) {
              return 'react-vendor';
            }
          }
        }
      }
    }

  }
})
